package com.sunday.volunteer.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.volunteer.R;

import java.util.List;

/**
 * Created by Pual's PC on 2016/10/9.
 */
abstract public class RecyclerViewAdapter<T> extends RecyclerView.Adapter{
    private T t;
    private int[] layout;
    private List<T> list;
    private Integer itemView;
    private boolean isList;
    protected View.OnClickListener onClickListener;
    public RecyclerViewAdapter(T t , int[] layout) {
        this.t = t;
        this.layout = layout;
        isList = false;
    }

    public RecyclerViewAdapter(List<T> list , int itemView) {
        this.list = list;
        this.itemView = itemView;
        isList = true;
    }

    public RecyclerViewAdapter(T t , int itemView ,boolean isList) {
        this.t = t;
        this.itemView = itemView;
        this.isList = isList;
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    abstract public class MyViewHolder<T> extends RecyclerView.ViewHolder{
        public MyViewHolder(View itemView) {
            super(itemView);
        }
        abstract public void bind(MyViewHolder holder ,T object,int position);
    }

    abstract public RecyclerView.ViewHolder onCreateViewHolder(View itemView , int viewType);

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =null;
        if(isList){
            view = LayoutInflater.from(parent.getContext()).inflate(itemView, null);
        }else {
            if(itemView!= null){
                view = LayoutInflater.from(parent.getContext()).inflate(itemView, null);
            }else {
                if(layout.length == 1){
                    view = LayoutInflater.from(parent.getContext()).inflate(layout[0], null);
                }else{
                    view = LayoutInflater.from(parent.getContext()).inflate(layout[viewType], null);
                }
            }
        }
        return onCreateViewHolder(view ,viewType);
    }

    @Override
    public int getItemViewType(int position) {
        if(layout != null){
            if(layout.length == 1){
                return -1;
            }else if(layout.length >1){
                for(int i =0 ;i<layout.length ;i++){
                    if(i == position){
                        return position;
                    }
                }
            }
        }
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder viewHolder = (MyViewHolder)holder;
        if(isList){
            if(list != null){
                viewHolder.bind(viewHolder,list.get(position) ,position);
            }
        }else {
            if(t != null){
                viewHolder.bind(viewHolder,t ,position);
            }
        }
    }

    /**
     * isList为false必须重写此方法
     * @return
     */
    @Override
    public int getItemCount() {
        if(isList){
            if(list == null){
                return 1;
            }else {
                return list.size();
            }
        }else {
            if(t != null){
                return 1;
            }
            return 0;
        }
    }

    public void setText(TextView text , Object object , String myContent){
        String content = String.valueOf(object);
        if(TextUtils.isEmpty(content)||content.equals("null")){
            text.setText(myContent);
        }else {
            text.setText(content);
        }
    }

    public void setText(TextView text , Object object){
        String content = String.valueOf(object);
        if(TextUtils.isEmpty(content) ||content.equals("null")){
            text.setText("");
        }else {
            text.setText(content);
        }
    }

    public void setTextFormat(TextView text ,String string1 ,String object){
        String content = String.valueOf(object);
        if(TextUtils.isEmpty(content)||content.equals("null")){
            text.setText(String.format("%s%s" , string1 , ""));
        }else {
            text.setText(String.format("%s%s" , string1 , content));
        }
    }

    public void setImage(ImageView imageView , String imagePath ,int width , int height){
        if(TextUtils.isEmpty(imagePath)){
            imagePath = "http";
         }
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Picasso.with(imageView.getContext()).load(imagePath).resize(width ,height).centerCrop().placeholder(R.drawable.default_error).into(imageView);
    }

}
